package Mark;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;


public class Client {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 8080);
            Scanner scanner = new Scanner(System.in)){
            System.out.println("已连接到服务器！");
            InputStream in = socket.getInputStream();
            OutputStream stream = socket.getOutputStream();
            while(true){
                String msg = scanner.nextLine();
                stream.write(msg.getBytes());
                //阻塞读取响应内容
                int len;
                byte[] buffer = new byte[1024];
                len = in.read(buffer);
                System.out.println(new String(buffer, 0, len));
            }

        }catch(IOException e){
            e.printStackTrace();
        }
        /* try-catch用于捕获异常，这里是捕获网络请求 */
    }
}
